package com.durian.base.frame.easyrouter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import com.durian.base.utils.StringUtils;
import com.durian.base.utils.ToolUtils;
import java.io.Serializable;
import java.util.ArrayList;


/**
 * 说明：构建工具
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2018/6/2 17:24
 * <p/>
 * 版本：version 1.0
 */
public class IntentBuilder {

    public static final String SUCCESS = "IntentBuilder&&SUCCESS";
    private Context mContext;
    private Intent intent;
    private boolean notNeedFinish = false;

    private IntentBuilder(Context context){
        this.mContext = context;
        intent = new Intent();
    }

    public static final IntentBuilder builder(Context context){
        return new IntentBuilder(context);
    }

    public IntentBuilder flags(int flags){
        intent.addFlags(flags);
        return this;
    }

    public IntentBuilder action(String action){
        if (StringUtils.isNotEmpty(action)){
            intent.setAction(action);
        }
        return this;
    }

    public IntentBuilder with(Bundle bundle){
        if (bundle != null){
            intent.putExtras(bundle);
        }
        return this;
    }

    public IntentBuilder with(Intent intent){
        if (intent != null){
            this.intent.putExtras(intent);
        }
        return this;
    }

    public IntentBuilder with(String key,String value){
        if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(value)){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,int value){
        if (StringUtils.isNotEmpty(key)){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,float value){
        if (StringUtils.isNotEmpty(key)){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,byte value){
        if (StringUtils.isNotEmpty(key)){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,char value){
        if (StringUtils.isNotEmpty(key)){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,short value){
        if (StringUtils.isNotEmpty(key)){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,double value){
        if (StringUtils.isNotEmpty(key)){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,boolean value){
        if (StringUtils.isNotEmpty(key)){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,long value){
        if (StringUtils.isNotEmpty(key)){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,int[] value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,long[] value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,byte[] value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,Character value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,Serializable value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(String key,Serializable[] value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(@NonNull String key,@NonNull Parcelable value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder with(@NonNull String key,@NonNull Parcelable[] value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder withSerializable(String key,Serializable value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder withSerializable(String key,Serializable[] value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder withParcelable(@NonNull String key,@NonNull Parcelable value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder withParcelable(@NonNull String key,@NonNull Parcelable[] value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putExtra(key,value);
        }
        return this;
    }

    public IntentBuilder withParcelable(String key,ArrayList<? extends Parcelable> value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putParcelableArrayListExtra(key,value);
        }
        return this;
    }

    public IntentBuilder withArrayInteger(String key,ArrayList<Integer> value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putIntegerArrayListExtra(key,value);
        }
        return this;
    }

    public IntentBuilder withArrayString(String key,ArrayList<String> value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putStringArrayListExtra(key,value);
        }
        return this;
    }

    public IntentBuilder withArrayChar(String key,ArrayList<CharSequence> value){
        if (StringUtils.isNotEmpty(key) && value != null){
            intent.putCharSequenceArrayListExtra(key,value);
        }
        return this;
    }

    public IntentBuilder className(String className){
        try {
            if (StringUtils.isNotEmpty(className)){
                Class.forName(className);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return this;
    }

    public IntentBuilder className(Class clazz){
        if (clazz != null){
            intent.setClass(mContext,clazz);
        }
        return this;
    }

    public IntentBuilder notNeedFinish(){
        notNeedFinish = true;
        return this;
    }

    public void setResult(int resultCode){
        if (mContext instanceof Activity){
            Activity activity = (Activity) mContext;
            if (ToolUtils.isNotFinish(activity)){
                with(SUCCESS,SUCCESS);
                activity.setResult(resultCode,build());
                if (!notNeedFinish){
                    activity.finish();
                }
            }
        }
    }

    public void setResult(){
        setResult(Activity.RESULT_OK);
    }

    public Intent build(){
        return intent;
    }

}
